
from wxb_settingsDialog import *
import utils
import os

class SettingsDialog(wxb_settingsDialog):
    
    def __init__(self, parent):
        super().__init__(parent)
        
        self.m_bpButton_formatpath.SetBitmap( wx.Bitmap( u"./assets/default_folder_opened.png", wx.BITMAP_TYPE_ANY ) )
        self.m_bpButton_dirlogpath.SetBitmap( wx.Bitmap( u"./assets/default_folder_opened.png", wx.BITMAP_TYPE_ANY ) )
        self.SetIcon(wx.Icon(".\\assets\\settings.ico"))
        
        self.__show_settings()

    def OnSettingsFinish(self, event):
        
        utils.CURRENT_SETTINGS["OFFSET"] = self.m_textCtrl_offset.GetValue()
        utils.CURRENT_SETTINGS["PREFIX"] = self.m_textCtrl_prefix.GetValue()
        utils.CURRENT_SETTINGS["DELIMITER"] = self.m_textCtrl_delimiter.GetValue()
        utils.CURRENT_SETTINGS["LEVEL_MARKER"] = self.m_textCtrl_levelmarker.GetValue()
        utils.CURRENT_SETTINGS["PRE_LEVEL"] = str(self.m_choice_prelevel.GetSelection() + 1)
        utils.CURRENT_SETTINGS["PRE_TITLE"] = self.m_textCtrl_pretitle.GetValue()
        utils.CURRENT_SETTINGS['PDF_OUTPUT_PATH'] = self.m_textCtrl_pdfoutputpath.GetValue()
        utils.CURRENT_SETTINGS['DIR_LOG_PATH'] = self.m_textCtrl_dirlogpath.GetValue()
        utils.CURRENT_SETTINGS["DIR_FORMAT_PATH"] = self.m_textCtrl_formatpath.GetValue()
        utils.IS_PRINT_ACTICE_ONLY = int(self.m_checkBox_adviceOnly.GetValue())
        utils.IS_FORMAT_DIR_FIRST = int(self.m_checkBox_formatfirst.GetValue())
        utils.IS_PRINT_PROCESS = int(self.m_checkBox_printprocess.GetValue())
        
        with wx.MessageDialog(self, "参数设置成功!", 'PDG', wx.OK | wx.ICON_INFORMATION) as dlg:
            dlg.ShowModal()
        
        event.Skip()
        return super().OnSettingsFinish(event)

    def __show_settings(self):
        self.m_textCtrl_offset.SetValue(utils.CURRENT_SETTINGS["OFFSET"])
        self.m_textCtrl_prefix.SetValue(utils.CURRENT_SETTINGS["PREFIX"])
        self.m_textCtrl_delimiter.SetValue(utils.CURRENT_SETTINGS["DELIMITER"])
        self.m_textCtrl_levelmarker.SetValue(utils.CURRENT_SETTINGS["LEVEL_MARKER"])
        self.m_choice_prelevel.SetSelection(int(utils.CURRENT_SETTINGS["PRE_LEVEL"])-1)
        self.m_textCtrl_pretitle.SetValue(utils.CURRENT_SETTINGS["PRE_TITLE"])
        self.m_textCtrl_pdfoutputpath.SetValue(utils.CURRENT_SETTINGS['PDF_OUTPUT_PATH'])
        self.m_textCtrl_dirlogpath.SetValue(utils.CURRENT_SETTINGS['DIR_LOG_PATH'])
        self.m_textCtrl_formatpath.SetValue(utils.CURRENT_SETTINGS["DIR_FORMAT_PATH"])
        self.m_checkBox_adviceOnly.SetValue(utils.IS_PRINT_ACTICE_ONLY)
        self.m_checkBox_formatfirst.SetValue(utils.IS_FORMAT_DIR_FIRST)
        self.m_checkBox_printprocess.SetValue(utils.IS_PRINT_PROCESS)

    def OnSettingsReset(self, event):
        
        for key in utils.DEFAULT_SETTINGS:
            if key in utils.CURRENT_SETTINGS:
                utils.CURRENT_SETTINGS[key] = utils.DEFAULT_SETTINGS[key]
        utils.IS_PRINT_ACTICE_ONLY = 1
        utils.IS_FORMAT_DIR_FIRST = 1
        utils.IS_PRINT_PROCESS = 1
        
        self.__show_settings()
        
        with wx.MessageDialog(self, "参数已恢复默认设置", 'PDG', wx.OK | wx.ICON_INFORMATION) as dlg:
            dlg.ShowModal()
        
        event.Skip()
        return super().OnSettingsReset(event)

    def __change_dirformatpath(self, newpath):
        utils.CURRENT_SETTINGS["DIR_FORMAT_PATH"] = newpath
        self.m_textCtrl_formatpath.SetValue(newpath)
        
    def OnbpClickDirFormatPath(self, event):
        
        with wx.DirDialog(self, "请选择格式化目录的输出文件夹 ...",
                          defaultPath=os.getcwd(),
                          style=wx.FD_OPEN) as dlg:
            if dlg.ShowModal() == wx.ID_OK:
                newpath = dlg.GetPath()
                self.__change_dirformatpath(newpath)
        
        event.Skip()
        return super().OnbpClickDirFormatPath(event)

    def __change_dirlogpath(self, newpath):
        utils.CURRENT_SETTINGS["DIR_LOG_PATH"] = newpath
        self.m_textCtrl_dirlogpath.SetValue(newpath)
    
    def OnbpClickDirLogPath(self, event):
        
        with wx.DirDialog(self, "请选择格式化目录文件的输出日志存放文件夹 ...",
                          defaultPath=os.getcwd(),
                          style=wx.FD_OPEN) as dlg:
            if dlg.ShowModal() == wx.ID_OK:
                newpath = dlg.GetPath()
                self.__change_dirlogpath(newpath)
        
        event.Skip()
        return super().OnbpClickDirLogPath(event)